// pages/dry/dry.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    number:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.goods_list()
    let car = options.car
    console.log(car,'car')
    if (car) {
      this.setData({ car })
    }

  },
  goods_list() {
    // app.get('goods_list', {}).then(res => {
    //   const { list } = res.data
    //   console.log(list,res.data,'list')
    //   if (this.data.car) {
    //     let list_index = list.indexOf(this.data.car)
    //     this.setData({ list_index: String(list_index) })
    //   }
    //   this.setData({ list })
    // })
    let args = wx.getStorageSync('args')
    let _this = this;
    let appInstance = getApp()
    wx.request({
      url: appInstance.globalData.requestUrl+'/wx/biz/get_goods',
      data: {
        typeId: ''
      },
      method:'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer '+ args.loginToken
      },
      success (res) {
        if (res.data.code == '401') {
          wx.navigateTo({
            url: '/pages/user/login/login',
          })
        } else {
          const list = res.data.data.map(item => {
            return item
          })
          if (_this.data.car) {
            let list_index = list.findIndex(item=>item.goodsName==_this.data.car)
            _this.setData({list_index: String(list_index)})
          }
          _this.setData({list})
          console.log(res, _this.data.list,'ress')
        }
      }
    })
  },
  change_list(e) {
    this.setData({ list_index: e.detail.value })
  },
  handle_name(e) {
    this.setData({ name: e.detail.value })
  },
  handle_phone(e) {
    this.setData({ phone: e.detail.value })
  },
  handle_code(e) {
    this.setData({ verifyCode: e.detail.value })
  },
  verifyCode(){
    let _this = this
    let appInstance = getApp()
    if(!_this.data.phone){
      wx.showToast({
        title: '请输入手机号',
        icon: "none"
      })
      return
    }
    if(_this.data.number >0){
      return;
    }
    this.setData({ number: 60 })
    var countdownId = setInterval(function() {
      if (_this.data.number > 0) {
        _this.data.number--;
        _this.setData({ number: _this.data.number })
      } else {
        console.log("倒计时结束");
        clearInterval(countdownId);
      }
    }, 1000);
    let args = wx.getStorageSync('args')
    wx.request({
      url: appInstance.globalData.requestUrl+'/wx/sms/send',
      data: {
        "phone": _this.data.phone, //手机号
      },
      method:'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer '+ args.loginToken
      },
      success (res) {
        if(res.data.code == '200'){
          wx.showToast({
            title: '发送成功',
            icon: "success"
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
      }
    })
  },
  add_dry() {
    if (!this.data.list_index) {
      wx.showToast({
        title: '请选择意向车型',
        icon: "none"
      })
    }
    else if (!this.data.name || !this.data.phone) {
      wx.showToast({
        title: '请完善个人信息',
        icon: "none"
      })
    }
    else {
      wx.showLoading({
        success: res => {
          let data = {
            goodsId: this.data.list[this.data.list_index].id,
            name: this.data.name,
            phone: this.data.phone,
            code: this.data.verifyCode
          }
          let args = wx.getStorageSync('args')
          let _this = this;
          let appInstance = getApp()
          wx.request({
            url: appInstance.globalData.requestUrl+'/wx/biz/add_dry',
            data: data,
            method:'POST',
            header: {
              'content-type': 'application/json', // 默认值
              'Authorization': 'Bearer '+ args.loginToken
            },
            success (res) {
              wx.hideLoading()
              wx.showToast({
                title: res.data.msg,
                icon: "none"
              })
            }
          })
          // app.get('add_dry', data).then(res => {
          //   console.log(res);
          //   wx.hideLoading()
          //   wx.showToast({
          //     title: res.msg,
          //     icon: "none"
          //   })
          // })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
